<%= form_for(post, action: post_path) do |f| %>

    <%= render 'shared/error_messages',object: post %>

    <div class="field">
      <%= f.label :title %>
      <%= f.text_field :title %>
    </div>

    <div class="field">
      <%= f.label :context %>
      <%= f.text_area :context,  :style => 'width: 500px; height:200px' %>
    </div>
    <%= image_tag post.picture.url if post.picture? %>
    <span class="picture">
      <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
    </span>

    <image id='image' src="<%= post.image %>"></image>
    <%= f.hidden_field :image, id: 'image_url' %>

    <span class="picture_url">
      <%= f.file_field :image, name: 'image_path', id: 'current_image_url', accept: 'image/jpeg,image/gif,image/png' %>
    </span>
    <div class="actions">
      <% if !post.attachment.nil? %>
          <%= link_to t(:file) , post.attachment ,id: 'file'%>
      <% end %>
    </div>
    <%= f.hidden_field :attachment %>
    <span>
      <%= f.file_field :attachment, name: 'attachment_path', id: 'attachment', accept: 'file/zip' %>
    </span>

    <div class="actions">
      <%= link_to t(:back), posts_path %>
      <%= f.submit value= t(:post) %>
    </div>

<% end %>

<script type="text/javascript">
    $('#post_picture').bind('change', function () {
        var size_in_megabytes = this.files[0].size / 1024 / 1024;
        if (size_in_megabytes > 5) {
            alert('Maximum file size is 5MB. Please choose a smaller file.');
        }
    });

    $("#image").bind('click', function () {
        $('#image').removeAttr("src");
        $('image_url').val('');
    })

    $(function () {
        //文件上传
        function fileUploadStart() {
            $('#attachment').uploadifySettings('scriptData', {
                'imgHeight': 128,
                'imgWidth': 128,
                'fixName': '*.zip',
                'imgType': 1
            });
        }

        function fileUploadSuccess(event, queueID, fileObj, response) {
            var res = JSON.parse(response);
            $('#post_attachment').val(res.image_url)
            $('#post_attachment').val(res.image_url)
            console.log(res)
        }

        $(function () {
            $.mitian.initSingleFileUploader2('#attachment',
                    "*.zip", 1024 * 1024, fileUploadStart,
                    fileUploadSuccess);
        });


        //图片上传
        function uploadStart() {
            $('#current_image_url').uploadifySettings('scriptData', {
                'imgHeight': 128,
                'imgWidth': 128,
                'fixName': '*.jpeg;*.jpg;*.png',
                'imgType': 1
            });
        }


        function uploadSuccess(event, queueID, fileObj, response) {
            var res = JSON.parse(response);
            $('#image').attr("src", res.image_url);
            $('#image_url').val(res.image_url)
            console.log(res)
        }

        $(function () {

            $.mitian.initSingleFileUploader('#current_image_url',
                    "*.jpeg;*.jpg;*.png", 1024 * 1024, uploadStart,
                    uploadSuccess);


        });
    })
</script>
